package com.gee.spring.boot.oauth2.auth.config.web;

import com.gee.spring.boot.oauth2.common.util.http.ResponseUtil;
import com.gee.spring.boot.oauth2.common.util.result.ResultCode;
import com.gee.spring.boot.oauth2.common.util.result.ResultUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * desc:
 *  无权限后的处理, 抛出AccessDeniedException的处理
 * @author gee
 * @date 2020/04/01 22:18
 */
@Component(value = "accessDeniedHandler")
public class GeeAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        ResponseUtil.writeJson(response, ResultUtil.error(ResultCode.UNAUTHENTICATED));
    }
}
